#region File Description
//-----------------------------------------------------------------------------
// OptionsMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.GamerServices;
using System;
#endregion

namespace UglyAnimals
{
    /// <summary>
    /// The options screen is brought up over the top of the main menu
    /// screen, and gives the user a chance to configure the game
    /// in various hopefully useful ways.
    /// </summary>
    class OptionsMenuScreen : MenuScreen
    {
        #region Fields

        MenuEntry animalMenuEntry;
        MenuEntry soundMenuEntry;
        MenuEntry musicMenuEntry;
        MenuEntry playerNameMenuEntry;

        static string[] animals = { "Naked Mole", "Sea Cow", "Turkey" };
        static int currentAnimal = 0;

        static bool soundEnabled = true;
        static bool musicEnabled = true;

        static string playerName;

        #endregion

        #region Initialization


        /// <summary>
        /// Constructor.
        /// </summary>
        public OptionsMenuScreen()
            : base("")
        {
            // Create our menu entries.
            animalMenuEntry = new MenuEntry(string.Empty);
            soundMenuEntry = new MenuEntry(string.Empty);
            musicMenuEntry = new MenuEntry(string.Empty);
            playerNameMenuEntry = new MenuEntry(string.Empty);

            StorageManager sm = StorageManager.GetInstance();
            currentAnimal = sm.SelectedAnimal;
            soundEnabled = sm.IsSoundEnabled;
            musicEnabled = sm.IsMusicEnabled;
            playerName = sm.PlayerName;

            SetMenuEntryText();

            // Hook up menu event handlers.
            animalMenuEntry.Selected += AnimalMenuEntrySelected;
            soundMenuEntry.Selected += SoundMenuEntrySelected;
            musicMenuEntry.Selected += MusicMenuEntrySelected;
            playerNameMenuEntry.Selected += PlayerNameMenuEntrySlected;

            // Add entries to the menu.
            MenuEntries.Add(animalMenuEntry);
            MenuEntries.Add(soundMenuEntry);
            MenuEntries.Add(musicMenuEntry);
            MenuEntries.Add(playerNameMenuEntry);
        }


        /// <summary>
        /// Fills in the latest values for the options screen menu text.
        /// </summary>
        void SetMenuEntryText()
        {
            animalMenuEntry.Text = "Preferred animal: " + animals[currentAnimal];
            soundMenuEntry.Text = "SoundFX: " + (soundEnabled ? "on" : "off");
            musicMenuEntry.Text = "Music: " + (musicEnabled ? "on" : "off");
            playerNameMenuEntry.Text = "Player Name: " + playerName;
        }

        #endregion

        #region Handle Input


        /// <summary>
        /// Event handler for when the Ungulate menu entry is selected.
        /// </summary>
        void AnimalMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            currentAnimal = (currentAnimal + 1) % animals.Length;

            SetMenuEntryText();
        }


        /// <summary>
        /// Event handler for when the Frobnicate menu entry is selected.
        /// </summary>
        void SoundMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            soundEnabled = !soundEnabled;

            SetMenuEntryText();
        }

        void MusicMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            musicEnabled = !musicEnabled;

            SetMenuEntryText();
        }

        void PlayerNameMenuEntrySlected(object sender, PlayerIndexEventArgs e)
        {
            IAsyncResult result = Guide.BeginShowKeyboardInput(PlayerIndex.One,
                "Player Name",
                "Enter the player name",
                playerName,
                GetTypedChars,
                null);
        }

        protected void GetTypedChars(IAsyncResult r)
        {
            playerName = Guide.EndShowKeyboardInput(r);
            SetMenuEntryText();
        }

        #endregion

        protected override void OnCancel(PlayerIndex playerIndex)
        {
            StorageManager sm = StorageManager.GetInstance();
            sm.SelectedAnimal = currentAnimal;
            sm.IsSoundEnabled = soundEnabled;
            sm.IsMusicEnabled = musicEnabled;
            sm.PlayerName = playerName;

            ExitScreen();
        }
    }
}
